package com.skit.ecm.admin.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.skit.common.ResourcesManager
import com.skit.common.bean.ActivityBean
import com.skit.common.utils.StatusUtils
import com.skit.ecm.admin.R
import kotlinx.android.synthetic.main.admin_main_re_item_activity_layout.view.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author shuike [Contact me.](mailto:shuike007@gmail.com)
 * @version 1.0
 * @since 2020/5/19
 */
class MainAdminActivityRecyclerViewAdapter(private val list: MutableList<ActivityBean>) :
    RecyclerView.Adapter<MainAdminActivityRecyclerViewAdapter.ViewHolder>() {
    private val format = SimpleDateFormat("yyyy.MM.dd", Locale.getDefault())

    var onItemClickListener: ((View, Int) -> Unit?)? = null

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.image
        val title: TextView = itemView.title
        val activity_start_time: TextView = itemView.activity_start_time
        val apply_time: TextView = itemView.apply_time
        val event_location: TextView = itemView.event_location
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflate =
            LayoutInflater.from(parent.context)
                .inflate(R.layout.admin_main_re_item_activity_layout, parent, false)
        return ViewHolder(inflate).apply {
            itemView.setOnClickListener {
                onItemClickListener?.invoke(it, bindingAdapterPosition)
            }
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bean = list[position];

        Glide.with(holder.itemView).load(bean.activityImages[0])
            .into(holder.imageView)
        holder.title.text = bean.activityName
        holder.event_location.text = ResourcesManager.getRes().getString(
            R.string.main_event_location, bean.eventLocation
        )

        holder.activity_start_time.text = ResourcesManager.getRes().getString(
            R.string.activity_time,
            format.format(bean.startTime),
            format.format(bean.endTime)
        )

        holder.apply_time.text = ResourcesManager.getRes().getString(
            R.string.apply_time,
            format.format(bean.signUpStartTime),
            format.format(bean.signUpEndTime)
        )


        holder.itemView.sponsor.text = bean.organizer?.departmentName

        holder.itemView.number_of_apply_2.text = ResourcesManager.getRes().getString(
            R.string.person_apply, bean.registeredNum
        )
        holder.itemView.number_of_apply.text = StatusUtils.statusText(bean.status)
        holder.itemView.number_of_apply.setBackgroundColor(
            StatusUtils.statusColor(
                holder.itemView.context,
                bean.status
            )
        )
//        holder.itemView.number_of_apply.text = ResourcesManager.getRes().getString(
//            R.string.main_xiegang, bean.registeredNum, bean.quotaNum
//        )

    }

    override fun getItemCount(): Int = list.size
}